package com.minsf.example.provider;

import com.minsf.example.common.model.User;
import com.minsf.example.common.service.UserService;
import com.minsf.rpc.HttpServer;
import com.minsf.rpc.RpcApplication;
import com.minsf.rpc.VertxHttpServer;
import com.minsf.rpc.config.RegistryConfig;
import com.minsf.rpc.config.RpcConfig;
import com.minsf.rpc.model.ServiceMetaInfo;
import com.minsf.rpc.registry.LocalRegistry;
import com.minsf.rpc.registry.Registry;
import com.minsf.rpc.registry.RegistryFactory;
import com.minsf.rpc.server.tcp.VertxTcpServer;

/**
 * @Description
 * @Author minify
 * @Date 2024/3/16 0:24
 * @Project sf-rpc
 */
public class EasyProviderExample {

    public static void main(String[] args) {
        // RPC框架初始化
        RpcApplication.init();

        String serviceName = UserService.class.getName();

        // 本地注册
        LocalRegistry.registry(serviceName, UserServiceImpl.class);

        // 注册服务到注册中心
        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        // 端口号为null
        serviceMetaInfo.setServiceAddress(rpcConfig.getServerHost()+":"+rpcConfig.getServerPort());
        serviceMetaInfo.setServiceName(serviceName);
        serviceMetaInfo.setServiceVersion("1.0");
        serviceMetaInfo.setServicePort(rpcConfig.getServerPort());


        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
        try {
            registry.register(serviceMetaInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 启动服务
        /*HttpServer httpServer = new VertxHttpServer();
        httpServer.doStart(rpcConfig.getServerPort());*/
        VertxTcpServer vertxTcpServer = new VertxTcpServer();
        vertxTcpServer.doStart(rpcConfig.getServerPort());
    }
}
